package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.PageHelper分页
        PageHelper.startPage(pageNum, pageSize);
        //2.调用mapper
        Page<NursingProjectVo> byPage = nursingProjectMapper.getByPage(name, status);
        //3.封装结果返回
        return PageResponse.of(byPage, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void delById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);//0禁用   1启用
        nursingProjectMapper.update(nursingProject);
    }
}
